package com.atung.tool;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * 
 * @author lyb <br>
 * 2018年4月12日 下午5:28:55
 */
public class DateTimeTool {
    
    /**
     * private 类型的构造器
     * 
     * 工具类 全部添加私有构造器，方法全部声明为static 
     */
    private DateTimeTool(){}
    /**
     * 日期格式yyyy-MM-dd
     */
    public static final String pattern_date = "yyyy-MM-dd";

    /**
     * 日期时间格式yyyy-MM-dd HH:mm:ss
     */
    public static final String pattern_date_time = "yyyy-MM-dd HH:mm:ss";
   
    /**
     * 日期时间格式 HH:mm:ss
     */
    public static final String pattern_time = "HH:mm:ss";
    //对象转字符串
    /** 1
     * 1. LocalDateTime 转为字符串
     */
    public static synchronized String localDateTimeToString(LocalDateTime localDateTime){
        if (localDateTime == null) {
            return null;
        }
        try {
            DateTimeFormatter formatter=DateTimeFormatter.ofPattern(pattern_date_time);
            return localDateTime.format(formatter);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(localDateTime + "转换成String类型失败，请检查数据来源");
        }
        return null;
    }
    
    /** 2
     * 2. LocalDate 转为字符串
     */
    public static synchronized String localDateToString(LocalDate localDate){
        if (localDate == null) {
            return null;
        }
        try {
            return localDate.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(localDate + "转换成String类型失败，请检查数据来源");
        }
        return null;
    }

    /** 3
     * 3. LocalTime 转为字符串
     */
    public static synchronized String localTimeToString(LocalTime localTime){
        if (localTime == null) {
            return null;
        }
        try {
            return localTime.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(localTime + "转换成String类型失败，请检查数据来源");
        }
        return null;
    }
    
    //字符串转对象
    
    /** 4
     * 1. 字符串类型日期转为 LocalDateTime 
     */
    public static synchronized LocalDateTime stringToLocaDateTime(String stringDateTime){
        if (stringDateTime == null) {
            return null;
        }
        try {
            DateTimeFormatter datetimeformatter = DateTimeFormatter.ofPattern(pattern_date_time);
            LocalDateTime localdatetime = LocalDateTime.parse(stringDateTime,datetimeformatter);
            return localdatetime;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(stringDateTime + "转换成LocalDateTime类型失败，请检查数据来源");
        }
        return null;
        
    } 
    
    /** 5
     * 2. 字符串类型日期转为 LocalDate 
     */
    public static synchronized LocalDate stringToLocaDate(String stringDate){
        if (stringDate == null) {
            return null;
        }
        try {
            LocalDate localdate = LocalDate.parse(stringDate, DateTimeFormatter.ofPattern(pattern_date));
            return localdate;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(stringDate + "转换成LocalDate类型失败，请检查数据来源");
        }
        return null;
        
    }
    
    /** 6
     * 3. 字符串类型日期转为 LocalTime 
     */
    public static synchronized LocalTime stringToLocaTime(String stringTime){
        if (stringTime == null) {
            return null;
        }
        try {
            LocalTime localtime = LocalTime.parse(stringTime,DateTimeFormatter.ofPattern(pattern_time));
            return localtime;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(stringTime + "转换成String类型失败，请检查数据来源");
        }
        return null;
        
    }
    
    //=====================================================
    
    /** 7
     * 毫秒数转为LocalDateTime日期
     */
    public static synchronized LocalDateTime millisToLocalDateTime(Long milliSeconds){
        if (milliSeconds == null) {
            return null;
        }
        try {
            DateTimeFormatter datetimeformatter = DateTimeFormatter.ofPattern(pattern_date_time);
            String StringmilliSeconds = datetimeformatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSeconds),ZoneId.of("Asia/Shanghai")));        
            LocalDateTime localdatetime = LocalDateTime.parse(StringmilliSeconds,datetimeformatter);
            return localdatetime;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(milliSeconds + "转换成LocalDateTime类型失败，请检查数据来源");
        }
        return null;
        
    }
    
    //对象转毫秒数
    
    /** 8
     * 把LocalDateTime日期转为毫秒数 
     */
    public static synchronized Long localDateTimeToMillis(LocalDateTime localDateTime){
        if (localDateTime == null) {
            return null;
        }
        try {
            long longdatetime = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
            return longdatetime;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(localDateTime + "转换成Long类型失败，请检查数据来源");
        }
        return null;
        
    }
 
    /** 9
     *  字符串转为毫秒数
     */
    public static synchronized Long stringToMillis(String stringDateTime){
        if (stringDateTime == null) {
            return null;
        }
        try {
            DateTimeFormatter datetimeformatter = DateTimeFormatter.ofPattern(pattern_date_time);
            LocalDateTime localdatetime = LocalDateTime.parse(stringDateTime,datetimeformatter);
            long longdatetime = localdatetime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
            return longdatetime;
        } catch (Exception ex) { 
            ex.printStackTrace();
            System.err.println(stringDateTime + "转换成Long类型失败，请检查数据来源");
        }
        return null;
       
    }
    
    /** 10 
     * 毫秒数 转为 字符串 年月日 时分秒
     */
    public static synchronized String millisToStringDateTime(Long milliSeconds){
        if (milliSeconds == null) {
            return null;
        }
        try {
            DateTimeFormatter datetimeformatter = DateTimeFormatter.ofPattern(pattern_date_time);
            String StringmilliSeconds = datetimeformatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSeconds),ZoneId.of("Asia/Shanghai")));        
            return StringmilliSeconds;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(milliSeconds + "转换成String类型失败，请检查数据来源");
        }
        return null;
        
    }
    
    /** 11
     * 毫秒数 转为 日期字符串 年月日
     */
    public static synchronized String millisToStringDate(Long milliSeconds){
        if (milliSeconds == null) {
            return null;
        }
        try {
            DateTimeFormatter datetimeformatter = DateTimeFormatter.ofPattern(pattern_date);
            String StringmilliSeconds = datetimeformatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSeconds),ZoneId.of("Asia/Shanghai")));        
            return StringmilliSeconds;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(milliSeconds + "转换成String类型失败，请检查数据来源");
        }
        return null;
       
    }
    
    /** 12
     * 毫秒数转为 日期字符串 时分秒
     */
    public static synchronized String millisToStringTime(Long milliSeconds){
        if (milliSeconds == null) {
            return null;
        }
        try {
            DateTimeFormatter datetimeformatter = DateTimeFormatter.ofPattern(pattern_time);
            String StringmilliSeconds = datetimeformatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSeconds),ZoneId.of("Asia/Shanghai")));        
            return StringmilliSeconds;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(milliSeconds + "转换成String类型失败，请检查数据来源");
        }
        return null;
        
    }

}
